package cn.e3.service.impl;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import cn.e3.mapper.TbItemDescMapper;
import cn.e3.mapper.TbItemMapper;
import cn.e3.pagehelper.PageHelperResult;
import cn.e3.pojo.TbItem;
import cn.e3.pojo.TbItemDesc;
import cn.e3.pojo.TbItemExample;
import cn.e3.pojo.TbItemExample.Criteria;
import cn.e3.service.ItemService;
import cn.e3.utils.E3Result;
import cn.e3.utils.IDUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class ItemServiceImpl implements ItemService{
	@Autowired
	private TbItemMapper tbItem;

	@Override
	public TbItem selectItemById(Long id) {
		// TODO Auto-generated method stub
		//根据主键查
		//TbItem item = tbItem.selectByPrimaryKey(id);
		//根据逆向工程生成的example查
		TbItemExample example=new TbItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		List<TbItem> list = tbItem.selectByExample(example);
		if(list!=null&&list.size()>0){
			
			return list.get(0);
		}
		return null;
	}
	@Override
	public PageHelperResult getItemsList(int page, int row) {
		//设置分页信息
		PageHelper.startPage(page, row);
		//创建item设置信息
		TbItemExample example=new TbItemExample();
		//查询全部
		List<TbItem> list = tbItem.selectByExample(example);
		//创建分页对象
		PageHelperResult result=new PageHelperResult();
		//pagehelp的分页处理
		PageInfo<TbItem> pageInfo=new PageInfo<TbItem>(list);
		//设置分页对象
		result.setTotal(pageInfo.getTotal());
		result.setRows(list);
		return result;
	}
	@Autowired
	private TbItemDescMapper tbItemDescMapper;
	@Autowired
	private JmsTemplate jmsTemplate;
	@Resource//根据id注入
	private Destination topicDestination;
	//添加商品
	public E3Result insertItem(TbItem item, String desc) {
		//设置上下状态 1上架，0下架
		item.setStatus((byte) 1);
		//设置id
		final long id = IDUtils.genItemId();
		item.setId(id);
		//设置时间
		item.setCreated(new Date());
		//设置更新时间 
		item.setUpdated(new Date());
		//创建itemdesc对象，接受富文本编辑器desc
		TbItemDesc tbItemDesc=new TbItemDesc();
		//设置描述
		tbItemDesc.setItemDesc(desc);
		//设置时间
		tbItemDesc.setCreated(new Date());
		//设置id
		tbItemDesc.setItemId(id);
		//设置更新时间
		tbItemDesc.setUpdated(new Date());
		//插入数据库
		tbItem.insert(item);
		tbItemDescMapper.insert(tbItemDesc);
		//将添加商品信息发送出去
		jmsTemplate.send(topicDestination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				return session.createTextMessage(id+"");
			}
		});
		return E3Result.ok();
	}
	@Override
	public TbItemDesc selectitemById(Long id) {
		TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(id);
		return tbItemDesc;
	}
	
}
